#!/usr/bin/python
# -*- coding: UTF-8 -*-

import dbus
import dbus.service
import dbus.glib
import gobject 

class Television(dbus.service.Object):

   def __init__(self, session_bus):
      dbus.service.Object.__init__(self, session_bus, '/Television')
      self.turned_on = False
      self.channel = 11

   @dbus.service.method(dbus_interface='br.org.cinlug.Turnable')
   def TurnOn(self):
      self.turned_on = True

   @dbus.service.method(dbus_interface='br.org.cinlug.Turnable')
   def TurnOff(self):
      self.turned_on = False

      # Emite o sinal de TV desligada.
      self.TelevisionOff()

   @dbus.service.method(dbus_interface='br.org.cinlug.Turnable')
   def IsOn(self):
      return self.turned_on

   @dbus.service.method(dbus_interface='br.org.cinlug.Television')
   def GetChannel(self):
      if self.turned_on:
         return self.channel
      else:
         return None

   @dbus.service.method(dbus_interface='br.org.cinlug.Television')
   def SetChannel(self, channel):
      if channel < 2 or not self.turned_on:
         print 'Must raise an error.'
      else:
         self.channel = channel

   @dbus.service.signal(dbus_interface='br.org.cinlug.Television')
   def TelevisionOff(self, message='Someone turned off the TV.'):
      pass


if __name__ == '__main__':
   session_bus = dbus.SessionBus()
   bus_name = dbus.service.BusName('br.org.cinlug', bus=session_bus)

   tv = Television(bus_name)

   mainloop = gobject.MainLoop()
   mainloop.run()
 

